package com.hypo.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hypo.business.domain.dto.AddGoodsDto;
import com.hypo.business.domain.dto.GoodsPageDto;
import com.hypo.business.domain.dto.ItemSpuDto;
import com.hypo.business.domain.dto.PageResultDto;
import com.hypo.business.domain.po.Brand;
import com.hypo.business.domain.po.ItemSku;
import com.hypo.business.domain.po.ItemSpu;
import com.hypo.business.domain.po.Supplier;
import com.hypo.business.mapper.BrandMapper;
import com.hypo.business.mapper.ItemSkuMapper;
import com.hypo.business.mapper.ItemSpuMapper;
import com.hypo.business.mapper.SupplierMapper;
import com.hypo.business.service.ItemSpuService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class ItemSpuServiceImpl extends ServiceImpl<ItemSpuMapper, ItemSpu> implements ItemSpuService {
    private final ItemSpuMapper itemSpuMapper;
    private final ItemSkuMapper itemSkuMapper;
    private final BrandMapper brandMapper;
    private final SupplierMapper supplierMapper;

    /**
     * 修改
     */
    @Transactional
    @Override
    public void updateGoods(AddGoodsDto dto) {
        ItemSpu itemSpu = BeanUtil.toBean(dto.getSpuInfo(), ItemSpu.class);
        itemSpu.setUpdateTime(LocalDateTime.now());
        itemSpuMapper.updateById(itemSpu);
        itemSkuMapper.delete(Wrappers.<ItemSku>lambdaQuery()
                .eq(ItemSku::getSpuId, itemSpu.getId()));
        extracted(dto, itemSpu);
    }

    /**
     * 批量插入sku
     */
    private void extracted(AddGoodsDto dto, ItemSpu itemSpu) {
        List<ItemSku> itemSkuList = new ArrayList<>();
        dto.getSkuList().forEach(sku -> {
            ItemSku itemSku = BeanUtil.toBean(sku, ItemSku.class);
            itemSku.setSpuId(itemSpu.getId());
            itemSku.setCreateTime(LocalDateTime.now());
            itemSku.setUpdateTime(LocalDateTime.now());
            itemSku.setIsDel(1);
            itemSku.setProductVolume(itemSku.getProductLength() * itemSku.getProductWidth() * itemSku.getProductHeight());
            itemSkuList.add(itemSku);
        });
        itemSkuMapper.insert(itemSkuList);
    }

    /**
     * 新增
     */
    @Transactional
    @Override
    public void addGoods(AddGoodsDto dto) {
        ItemSpu itemSpu = BeanUtil.toBean(dto.getSpuInfo(), ItemSpu.class);
        itemSpu.setCreateTime(LocalDateTime.now());
        itemSpu.setUpdateTime(LocalDateTime.now());
        itemSpu.setIsDel(1);
        itemSpuMapper.insert(itemSpu);
        extracted(dto, itemSpu);
    }


    /**
     * 分页查询所有商品
     */
    @Override
    public PageResultDto<ItemSpuDto> goodsPage(GoodsPageDto dto) {
        // 创建分页对象
        Page<ItemSpu> page = new Page<>(dto.getPage(), dto.getPageSize());

        // 执行分页查询
        Page<ItemSpu> itemSpuPage = itemSpuMapper.selectPage(page, Wrappers.<ItemSpu>lambdaQuery()
                .like(!Objects.isNull(dto.getTitle()), ItemSpu::getTitle, dto.getTitle())
                .eq(!Objects.isNull(dto.getCategoryId3()), ItemSpu::getCategoryId3, dto.getCategoryId3()));

        // 转换数据
        List<ItemSpuDto> list = new ArrayList<>();
        itemSpuPage.getRecords().forEach(itemSpu -> {
            ItemSpuDto itemSpuDto = BeanUtil.toBean(itemSpu, ItemSpuDto.class);
            itemSpuDto.setChildren(itemSkuMapper.selectList(Wrappers.<ItemSku>lambdaQuery()
                    .eq(ItemSku::getSpuId, itemSpu.getId())));
            list.add(itemSpuDto);
        });

        // 封装分页结果
        PageResultDto<ItemSpuDto> result = new PageResultDto<>();
        result.setTotal(itemSpuPage.getTotal());
        result.setRows(list);
        return result;
    }

    /**
     * 查询所有渠道商
     *
     * @return
     */
    @Override
    public Object suppliers() {
        return supplierMapper.selectList(Wrappers.<Supplier>lambdaQuery()
                .eq(Supplier::getIsDel, 1));
    }



    /**
     * 查询所有品牌
     */
    @Override
    public Object brands() {
        return brandMapper.selectList(Wrappers.<Brand>lambdaQuery()
                .eq(Brand::getIsDel, 1));
    }


}
